/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.Reception;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.GraphNodeWithSemanticEditPart;
import com.cea.papyrus.diagram.classdiagram.figures.ReceptionFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Control class for {@link Reception}.
 */
public class ReceptionEditPart extends GraphNodeWithSemanticEditPart {

//	/**
//	 * Manager for editing operation name.
//	 */
//	protected DirectEditManager manager;
//
//	/**
//	 * Manager of lightweight label controlers.
//	 */ 
//	private LabelControlerManager labelsMngr;

	/**
	 * Creates a new ReceptionEditPart 
	 * @param graphNode parent of the graph node controlled by this edit part
	 * @param modelManager manager in charge of models, graphic and domain
	 */
	public ReceptionEditPart(GraphNode graphNode, ModelManager modelManager) {
		super(graphNode,modelManager);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected IFigure createFigure() {
		return new ReceptionFigure();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Label getNameLabel() {
		return ((ReceptionFigure)getFigure()).getNameLabel();
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public org.eclipse.uml2.uml.Reception getUmlElement(){
		return (org.eclipse.uml2.uml.Reception)((Uml1SemanticModelBridge)(getGraphNode()).getSemanticModel()).getElement();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void refreshVisuals() {

		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p ,size);
	
		// Retrieve current class and figure
		org.eclipse.uml2.uml.Reception currentReception = getUmlElement();
		com.cea.papyrus.umlutils.Element currentElement = new com.cea.papyrus.umlutils.Element(getUmlElement());

		ReceptionFigure fig = (ReceptionFigure)getFigure();
		 fig.refreshColors(getGraphNode());
		 fig.refreshPresentation(getPresentation(), currentReception, getGraphNode());
		boolean hasIcon  = currentElement.hasIcon();
		boolean hasShape = currentElement.hasShape();
		
		String stereotypes;
		stereotypes = horizontalStereotypesToDisplay();
		fig.refreshStereotypes(stereotypes, getPresentation(), hasIcon, hasShape);
		fig.setStereotypes(stereotypes);

		// If signal existe show signal name instead of reception name...
		if (currentReception.getSignal() == null) {
			fig.setName("undefined");
		} else {
			fig.setName(currentReception.getSignal().getName());
		}
		
		((GraphicalEditPart) getParent()).setLayoutConstraint(
				this,
				fig,
				r);

	}
//
//	/**
//	 * Gets the property figure.
//	 * 
//	 * @return the property figure
//	 */
//	protected OperationFigure getPropertyFigure(){
//		return (OperationFigure)createFigure() ;
//	}
//
//
//	/**
//	 * Init the labels Manager and register label controlers.
//	 * This method should be called when a Direct Edit request is performed.
//	 */
//	protected void initLabelControlerManager() {
//		if( labelsMngr != null ) {
//			return;
//		}
//
//		// Create and init it.
//		labelsMngr = new LabelControlerManager();
//		labelsMngr.createLabelControler( ((ReceptionFigure)getFigure()), 
//				new PropertyAccessor(){
//			public String getValue() { return getUmlElement().getName(); }
//			public void setValue(String value) {
//				getUmlElement().setName(value); 
//			}
//		});
//	}

	/**
	 * Returns the presentation of the node (custom label).
	 * @return the string corresponding to the representation of the node
	 */
	@Override
	public String getPresentation() {
		String presentation = getGraphNode().getSemanticModel().getPresentation();
		// default value = "". it is set to null by the tool
		if(presentation == null) {
			presentation = "";
			setPresentation(presentation);
		}
		return presentation;
	}

	/**
	 * Set the new presentation for the node, then refresh display.
	 * @param presentation the new presentation
	 */
	public void setPresentation(String presentation) {
		getGraphNode().getSemanticModel().setPresentation(presentation);
		refresh();
	}
	
//	/**
//	 * Init the label direct edit manager.
//	 * This method should be called when a Direct Edit request is performed.
//	 */
//	protected void initLabelEditManager() {
//		if(manager==null) {
//			// locator for the cell editor (on the name label)
//			CellEditorLocator locator = new SimpleCellEditorLocator(getFigure());
//
//			// defining how properties are modified in the model
//			PropertyAccessor accessor = new PropertyAccessor() {
//				public String getPropertyName() {
//					return "Name";
//				}
//				public String getValue() {
//					return getUmlElement().getName();
//				}
//				public void setValue(String value) {
//					getUmlElement().setName(value);  
//				}
//			};
//			// defines the controler: link between model and view
//			ILabelControler labelControler = new FigureControler(getFigure(), accessor);
//
//			// creates the editing manager.
//			manager = new LabelDirectEditManager(this, locator, labelControler);
//		}
//	}
//	
//	
//	/**
//	 * the direct edit mechanism requires the edit part to handle the direct edit
//	 * request.
//	 * 
//	 * @param req the request that called the direct edit.
//	 */
//	public void performDirectEdit(Request req) {
//		initLabelEditManager();
//		manager.show();
//	}
//
//	/* (non-Javadoc)
//	 * @see org.eclipse.gef.editparts.AbstractEditPart#performRequest(org.eclipse.gef.Request)
//	 */
//	/**
//	 * 
//	 * 
//	 * @param request 
//	 */
//	@Override
//	public void performRequest(Request request){
//		if (RequestConstants.REQ_DIRECT_EDIT.equals(request.getType())) {
//			performDirectEdit(request);
//		}
//	}
}